package ckR

import (
	cp "db2s/componentInit"
	"db2s/global"
	"db2s/parDef"
	mq "db2s/topic-mq"
	"strings"
)
import "sync/atomic"

func tableObjectInit(plan2 *SchedulePlan2) (tableObject string) {
	switch {
	case len(plan2.Object.PartName) > 0 && !strings.Contains(strings.ToLower(strings.Join(plan2.Object.PartName, ",")), "single"):
		tableObject = "P"
	default:
		tableObject = "N"
	}
	return
}
func indexObjectInit(plan2 *SchedulePlan2) {
	var indexMe parDef.IndexColumnMe
	switch plan2.baseMe.Index.(type) {
	case parDef.IndexColumnMe:
		indexMe = plan2.baseMe.Index.(parDef.IndexColumnMe)
	default:
		indexMe = parDef.IndexColumnMe{IndexName: "miss", ColumnKey: "M"}
	}
	plan2.TPod.IndexName = indexMe.IndexName
	plan2.TPod.IndexType = indexMe.ColumnKey
	plan2.TPod.IndexCol = strings.Join(func() (res []string) {
		if len(indexMe.ColumnMate) == 0 {
			res = []string{"none"}
			return
		}
		for _, v := range indexMe.ColumnMate {
			res = append(res, v.ColumnName)
		}
		return
	}(), ",")
	plan2.subTaskInfo.IndexName.Store(indexMe.IndexName)
	plan2.subTaskInfo.IndexCardinality.Store(indexMe.Cardinality)
	plan2.subTaskInfo.IndexObject.Store(indexMe.ColumnKey)
	return
}
func numericalValueInit(plan2 *SchedulePlan2) {
	var (
		selectCount, insertCount, recordCount   int64 = 0, 0, 0
		taskStatus, beginTime, outlierDetection int64 = 1, 0, 0
	)
	plan2.subTaskInfo.SAccumulate = &selectCount
	plan2.subTaskInfo.RecordCount = &recordCount
	plan2.subTaskInfo.DAccumulate = &insertCount
	plan2.subTaskInfo.TaskStatus = &taskStatus
	plan2.subTaskInfo.BeginTime = &beginTime
	plan2.subTaskInfo.OutlierDetection = &outlierDetection
}
func baseMetaInit(plan2 *SchedulePlan2) {
	var (
		ErrorInfo     atomic.Value
		partitionName atomic.Value
	)
	plan2.subTaskInfo = &mq.SubTasks{
		Schema:        plan2.Object.Schema,
		Table:         plan2.Object.Table,
		Loc:           plan2.TableLocNumber,
		ErrorInfo:     ErrorInfo,
		PartitionName: partitionName,
		TableObject:   tableObjectInit(plan2),
	}
	plan2.subTaskInfo.PartitionName.Store("single")
	ErrorInfo.Store("")
}
func topicCountInit(plan2 *SchedulePlan2) {
	var c = global.GetIoc().GetBean("globalConfig").(*GlobalParametersConfig)
	plan2.subTaskInfo.TopicCount = func() (tc map[string]*int64) {
		tc = make(map[string]*int64)
		for _, v1 := range GetCheckToTopicMQListeners()[cp.CheckMode(c.rules.CheckMode)] {
			var ff int64 = 0
			tc[string(v1.TopicName)] = &ff
		}
		return
	}()
}
func subTaskInit(plan2 *SchedulePlan2) {
	baseMetaInit(plan2)
	numericalValueInit(plan2)
	topicCountInit(plan2)
	indexObjectInit(plan2)
	PlanContext.TaskBarSubsStatusSend(plan2.subTaskInfo)
}

func (context *SchedulePlanContext) SubTaskBarAccumulate(n any) {
	context.TaskBarTable.BarAccumulate(n)
}
func (context *SchedulePlanContext) SpeedTaskBarAccumulate(n any) {
	context.TaskBarTable.SpeedAccumulate(n)
}
func (context *SchedulePlanContext) taskBarEndTopicAdd(n any) {
	context.TaskBarTable.TopicBarAdd(n)
}
func (context *SchedulePlanContext) TaskBarEndTopicDel(n any) {
	context.TaskBarTable.TopicBarDel(n)
}
func (context *SchedulePlanContext) TaskBarSubsStatusSend(table *mq.SubTasks) {
	context.TaskBarTable.BarSubsStatusSwapSend(table)
}
func (context *SchedulePlanContext) TaskBarSubsStatusSwap(table *mq.SubTasks, r string) {
	context.TaskBarTable.BarSubsStatusSwap(table, r)
}
func (context *SchedulePlanContext) TaskBarSubsValueSwap(table *mq.SubTasks, s, r string) {
	context.TaskBarTable.BarSubsValueSwap(table, s, r)
}
func (context *SchedulePlanContext) TaskBarSubsErrorSwap(table *mq.SubTasks, r string) {
	context.TaskBarTable.BarSubsErrorSwap(table, r)
}
func (context *SchedulePlanContext) TaskBarSubsStatusGet(table *mq.SubTasks) int64 {
	return context.TaskBarTable.BarSubsStatusGet(table)
}
func (context *SchedulePlanContext) TPodTableStatusSwap(status *int64, r string) {
	switch r {
	case "error":
		atomic.SwapInt64(status, -1)
	case "abnormal":
		atomic.SwapInt64(status, -2)
	case "finish":
		atomic.SwapInt64(status, 0)
	}
}
